package edu.sdust.ssmstudy.controller;

import com.google.gson.GsonBuilder;
import edu.sdust.ssmstudy.json.BaseJson;
import edu.sdust.ssmstudy.json.PaInfo;
import edu.sdust.ssmstudy.json.SelectJson;
import edu.sdust.ssmstudy.pojo.*;
import edu.sdust.ssmstudy.service.JobService;
import edu.sdust.ssmstudy.util.GsonFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/*List<Job> SelectByCity(String city);
   *//* List<Integer> SelectByAllID();
    String SelecttradesByID(Integer id);
    String SelectTitleByID(Integer id);*//*
        List<Job> GetmaxofferTop();
        List<String> SelectDifCity();
        Integer GetCityCount(String city);*/


/*<select id="GetmaxofferTop" resultMap="BaseResultMap">
        select * from job order by maxoffer desc limit 10
        </select>
        <select id="SelectDifCity" resultType="java.lang.String">
        select distinct city from job
        </select>
        <select id="GetCityCount" resultType="java.lang.Integer" parameterType="java.lang.String">
        select count(*) from job where city= #{city,jdbcType=VARCHAR}
        </select>*/
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
public class JobController {
    @Autowired
    private JobService jobService;
    //获取全部职位信息
    @ResponseBody
    @RequestMapping(value="/list", produces = "application/json;charset=UTF-8")
    public String list(){
        Jobs job = jobService.SelectById(1);
        List<Jobs> jobs = new ArrayList<Jobs>();
        jobs.add(job);
        jobs.add(jobService.SelectById(2));
        jobs.add(jobService.SelectById(3));
        String s = new GsonBuilder().create().toJson(jobs);
        return s;

    }
    //获取所有城市
    @ResponseBody
    @RequestMapping(value="/li",produces = "application/json;charset=UTF-8")
    public String Lis(){
        List<Trades> cityData = jobService.GetCityCount();
       String s = new GsonBuilder().create().toJson(cityData);
        return s;
    }
    //获取所有的职位信息
    @ResponseBody
    @RequestMapping(value="all",produces = "application/json; charset=UTF-8")
    public String GetAll(){
        List<Jobs> job = jobService.SelectAll();
        String s = new GsonBuilder().create().toJson(job);
        return s;
    }
    //获取薪资前十的职位
    @ResponseBody
    @RequestMapping(value="GetTop", produces = "application/json; charset=UTF-8")
    public String GetTopTen(){
        List<Jobs> top = jobService.GetTopTen();
        String s = new GsonBuilder().create().toJson(top);
        return s;
    }
    //获取不同的城市信息
    @ResponseBody
    @RequestMapping(value="GetDifCity", produces = "application/json; charset=UTF-8")
    public String GetDifCity(){
        List<String> city = jobService.SelectDifCity();
        //String s = new GsonBuilder().create().toJson(city);
        String s = new GsonBuilder().create().toJson(city);
        return s;
    }
    //获取北京的职位数量
    @ResponseBody
    @RequestMapping(value="GetCount", produces = "application/json; charset=UTF-8")
    public String GetCount(){
        String s =jobService.CityCount("北京").toString();
        return s;
    }

    //----------------------------以下方法都需要使用post来调用，不能通过在浏览器上写URL的方式访问！！------------------------------
    //获取所有信息
    @ResponseBody
    @RequestMapping(value="/listall",produces="application/json;charset=UTF-8")
    public String listAll(@RequestBody BaseJson baseJson) {
        return GsonFactory.getDefaultDateFormatGson().toJson(jobService.findAll(baseJson));
    }
    //查找
    @ResponseBody
    @RequestMapping(value="/find",produces="application/json;charset=UTF-8")
    public String find(@RequestBody SelectJson selectJson) {
        return GsonFactory.getDefaultDateFormatGson().toJson(jobService.find(selectJson));
    }
    //获取职位前十的城市
    @ResponseBody
    @RequestMapping(value="getTrades",produces = "application/json; charset=UTF-8")
    public String GetT(@RequestBody SelectJson selectJson){
        //String trad = trades.toString();
        List<Trades> tra = jobService.GetTopCityTrades(selectJson.getName());
        String s = new GsonBuilder().create().toJson(tra);
        return s;
    }
    //获取工作经验=============该方法需要用post 不能直接访问
    @ResponseBody
    @RequestMapping(value="GetExInfo", produces = "application/json; charset=utf-8")
    public String GetExInfo(@RequestBody SelectJson selectJson){
        List<Trades> ex = jobService.GetWxCount(selectJson.getName());
        String s = new GsonBuilder().create().toJson(ex);
        return  s;
    }
    //获取工作经验=============该方法需要用post 不能直接访问
    @ResponseBody
    @RequestMapping(value="GetInfo", produces = "application/json; charset=utf-8")
    public String GetInfo(){
        List<Trades> ex = jobService.GetWxCount("大数据");
        String s = new GsonBuilder().create().toJson(ex);
        return  s;
    }
    //分页查询
    @ResponseBody
    @RequestMapping(value="GetPageInfo", produces = "application/json; charset=UTF-8")
    public String GetPageInfo(@RequestBody PaInfo pageInfo){
        //PaInfo pageInfo  = new PaInfo(1,10,1);
        List<Jobs> jobs = jobService.GetPageInfo(pageInfo);
        String s = new GsonBuilder().create().toJson(jobs);
        return s;
    }
    @ResponseBody
    @RequestMapping(value="GetPaInfo", produces = "application/json; charset=UTF-8")
    public String GetPagInfo(){
        PaInfo pageInfo  = new PaInfo(1,10,1,"");
        List<Jobs> jobs = jobService.GetPageInfo(pageInfo);
        String s = new GsonBuilder().create().toJson(jobs);
        return s;
    }
    @ResponseBody
    @RequestMapping(value="GetTradesHot", produces = "application/json;charset=UTF-8")
    public String GetTradesHot(@RequestBody SelectJson selectJson){
        List<Trades> tra = jobService.GetTradesHot(selectJson.getName());
        String s = new GsonBuilder().create().toJson(tra);
        return s;
    }
    @ResponseBody
    @RequestMapping(value="GetMaxTop", produces = "application/json; charset=UTF-8")
    public String GetMaxTop(@RequestBody SelectJson selectJson){
        List<Money> mt = jobService.GetMaxTop(selectJson.getName());
        String s = new GsonBuilder().create().toJson(mt);
        return s;
    }
    @ResponseBody
    @RequestMapping(value="GetMTop", produces = "application/json; charset=UTF-8")
    public String GetMaxTop(){
        List<Money> mt = jobService.GetMaxTop("大数据");
        String s = new GsonBuilder().create().toJson(mt);
        return s;
    }
    @ResponseBody
    @RequestMapping(value="TradesInfo",produces = "application/json;charset=UTF-8")
    public String TradesInfo(@RequestBody SelectJson selectJson){
        List<String> arra = selectJson.getTrade();
        List<Trades> tr = new ArrayList<Trades>();
        for(int i = 0; i < arra.size(); i++){
            Trades tra = jobService.TradesInfo(arra.get(i));
            tr.add(tra);
        }
        String s = new GsonBuilder().create().toJson(tr);
        return s;
    }
    @ResponseBody
    @RequestMapping(value="TitleInfo",produces = "application/json;charset=UTF-8")
    public String TitleInfo(@RequestBody SelectJson selectJson){
        List<String> ara = selectJson.getTrade();
        List<Trades> tr = new ArrayList<Trades>();
        for(int i = 0; i < ara.size(); i++){
            Trades tra = jobService.TitleInfo(ara.get(i));
            tr.add(tra);
        }
        String s = new GsonBuilder().create().toJson(tr);
        return s;
    }
    @ResponseBody
    @RequestMapping(value="TradeInfo",produces = "application/json;charset=UTF-8")
    public String TradeInfo(){
        Trades tra = jobService.TradesInfo("金融");
        List<Trades> tr = new ArrayList<Trades>();
        tr.add(tra);
        String s = new GsonBuilder().create().toJson(tr);
        return s;

    }
}
